﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;

public partial class CRM_Store_ERP_InventoryEdit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["loadaction"] = "0";
            int c = BindCenterStore(Store_Rome, Request.QueryString["ptabid"]);
            if (c > 0)
                extcomb_StoreRoomId.SelectedIndex = 0;
            string objid = ViewState_ObjID = string.IsNullOrEmpty(Request.QueryString["ObjId"]) ? "Add" : Request.QueryString["ObjId"];
            ShowEditInfo(objid);
        }
    }

    protected void extBtn_Load_Click(object sender, DirectEventArgs e)
    {
        ViewState["loadaction"] = "1";
        GridBind();
    }

    private void GridBind()
    {
        DataTable dt = null;
        dt = CRMMis.BLL.ERP_StockInfo.GetStockList("1=1").Tables[0];
        Store1.DataSource = dt;
        Store1.DataBind();
        ViewState["dtList"] = dt;
        dt.Dispose();
    }

    protected void StoreChange(object sender, DirectEventArgs e)
    {
        if (ViewState["loadaction"].ToString() == "1")
        {
            GridBind();
        }
    }

    private void ShowEditInfo(string objid)
    {
        DataTable dtList = null;
        if (objid == "Add")
        {
            exttxtf_InUserName.Text = BS_LoginUserName;
            exthidn_InUser.Text = BS_LoginUserID;
            exttxtf_InDate.Text = DateTime.Now.ToDateTimeString();
        }
        else
        {
            var InventoryModel = CRMMis.BLL.ERP_Inventory.GetList("Inventoryid=" + objid).Tables[0];
            if (InventoryModel.Rows.Count > 0)
            {
                DataRow row = InventoryModel.Rows[0];
                int State = Convert.ToInt32(row["State"]);
                //根据状态控制
                if (State != 1)
                    extBtn_CheckInfo.Hidden = true;
                if (State > 0)
                {
                    extBtn_SubmitInfo.Hidden = true;
                    extBtn_Save.Hidden = true;
                    SetContainerReadOnly(formPanel, true);
                }
                if (State > 1)
                {
                    extBtn_CheckInfo.Hidden = true;
                }

                SetContainer(formPanel, InventoryModel);

                //绑定详情列表
                dtList = CRMMis.BLL.ERP_InventoryDetail.GetList(" Inventoryid=" + objid, "OrderId").Tables[0];
                Store1.DataSource = dtList;
                Store1.DataBind();
                ViewState["dtList"] = dtList;
                dtList.Dispose();
            }
        }
    }

    [DirectMethod]
    public void SaveData(int type)
    {
        CRMMis.Model.ERP_Inventory InventoryModel;
        if (ViewState_ObjID == "Add")
        {
            InventoryModel = new CRMMis.Model.ERP_Inventory();
            InventoryModel.Inventoryid = CRMMis.BLL.ERP_Inventory.GetNewID();
            InventoryModel.InventoryNo = GetBusinessNo("PD", "ERP_InventoryPD", Convert.ToInt32(extcomb_StoreRoomId.Value).ToString("000"));
            InventoryModel.StoreRoomId = int.Parse(extcomb_StoreRoomId.SelectedItem.Value);
            InventoryModel.InUser = BS_LoginUserID;
            InventoryModel.InDate = exttxtf_InDate.Text.Trim();
            InventoryModel.State = 0;
            InventoryModel.Remark = exttxtf_Remark.Text.Trim();
            if (type == 1)
                InventoryModel.State = 1;    //提交
        }
        else
        {
            InventoryModel = CRMMis.BLL.ERP_Inventory.GetModel(Convert.ToInt32(ViewState_ObjID));
            if (type == 0)
            {
                InventoryModel.StoreRoomId = int.Parse(extcomb_StoreRoomId.SelectedItem.Value);
                InventoryModel.Remark = exttxtf_Remark.Text.Trim();
                InventoryModel.State = 0;
            }
            if (type == 1)
            {
                if (InventoryModel.State == 0)
                {
                    InventoryModel.StoreRoomId = int.Parse(extcomb_StoreRoomId.SelectedItem.Value);
                    InventoryModel.Remark = exttxtf_Remark.Text.Trim();
                    InventoryModel.State = 1;   //提交
                }                    
                else
                {
                    X.Msg.Alert("系统提示", "提交失败！当前记录已经提交").Show();
                    return;
                }
            }
            else if (type == 2)
            {
                if (InventoryModel.State == 1)
                {
                    InventoryModel.CheckUser = BS_LoginUserID;
                    InventoryModel.CheckDate = System.DateTime.Now.ToString();
                    InventoryModel.State = 2;    //审核
                }
                else
                {
                    X.Msg.Alert("系统提示", "审核失败！当前记录已经审核").Show();
                    return;
                }
            }

        }

        CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
        myTrans.BeginTransaction();
        try
        {
            if (ViewState_ObjID == "Add")
            {
                CRMMis.BLL.ERP_Inventory.Add(myTrans, InventoryModel);
            }
            else
            {
                CRMMis.BLL.ERP_Inventory.Update(myTrans, InventoryModel);
                //删除子表
                if (type < 2)
                    CRMMis.BLL.ERP_InventoryDetail.DeleteFK(myTrans, "Inventoryid = " + InventoryModel.Inventoryid);
            }
            DataTable dtlist = ViewState["dtList"] as DataTable;
            if (type < 2)
            {
                int i = 1;
                foreach (DataRow item in dtlist.Rows)
                {
                    CRMMis.Model.ERP_InventoryDetail detailModel = new CRMMis.Model.ERP_InventoryDetail();
                    detailModel.ItemId = CRMMis.BLL.ERP_InventoryDetail.GetNewID();
                    detailModel.Inventoryid = InventoryModel.Inventoryid;
                    detailModel.OrderId = i;
                    detailModel.Count = Convert.ToInt32(item["Count"]);
                    detailModel.DetailId = Convert.ToInt32(item["DetailId"]);

                    CRMMis.BLL.ERP_InventoryDetail.Add(myTrans, detailModel);
                    i++;
                }
            }
            myTrans.CommitTran();
            ViewState_ObjID = InventoryModel.Inventoryid.ToString();
            exttxtf_InventoryNo.Text = InventoryModel.InventoryNo;

            string handler = "";
            if (type > 0)
                handler = "closeCurrTab();";
            X.Msg.Alert("系统提示", "保存成功！", handler).Show();
        }
        catch (Exception ex)
        {
            myTrans.RollBackTran();
            X.Msg.Alert("系统提示", "保存失败！</br>" + ex.Message).Show();
        }
    }




}